package com.ddwl.order.dao;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.exception.SqlDataException;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.order.dao.mapper.SeckillItemMapper;
import com.ddwl.order.dao.model.SeckillItem;
import com.ddwl.schema.bo.order.QuerySeckillBo;
import com.ddwl.schema.vo.order.SeckillItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
 * (AfterSaleOrder)表数据库访问层
 *
 * @author wenbin
 */
@Slf4j
@Repository
public class SeckillItemDao extends TkDao<SeckillItemMapper, SeckillItem> {

    /**
     * 查询秒杀活动商品列表
     * @return
     */
    public List<SeckillItem> querySeckillItemList(QuerySeckillBo bo){
        Example example = getExample();
        example.and().andEqualTo("seckillId",bo.getSeckillId())
                .andEqualTo("id", bo.getId())
                .andIn("seckillId",bo.getSeckillIds())
                .andLike("itemName", MySqlUtil.allLike(bo.getItemName()))
                .andEqualTo("itemId",bo.getItemId())
                .andEqualTo("barcode",bo.getBarcode());
        if (bo.getIsReleaseStock()!=null) {
            if (bo.getIsReleaseStock()==1) {
                example.and().andCondition(" is_release_stock is not null and is_release_stock = 1 ");
            } else {
                example.and().andCondition(" is_release_stock is  null or is_release_stock = 0 ");
            }
        }
        List<SeckillItem> seckillItems = baseMapper.selectByExample(example);
        return seckillItems;
    }

    /**
     * 更新秒杀商品售卖数量(修改活动重置售卖数量用)
     * @return
     */
    public void updateSeckillItemSale(QuerySeckillBo bo){
        SeckillItem seckillItem =  new SeckillItem();
        seckillItem.setSeckillSaleCount(bo.getSaleCount());
        seckillItem.setGmtModified(Instant.now());
        seckillItem.setId(bo.getSeckillId());
        Example example = new Example(SeckillItem.class);
        example.and()
                .andEqualTo("seckillId",bo.getSeckillId())
                .andEqualTo("barcode",bo.getBarcode());
        int i = baseMapper.updateByExampleSelective(seckillItem, example);
/*        if(i<1){
            log.error("更新秒杀数据失败 seckillId={}", JSON.toJSONString(bo.getSeckillId()));
            throw new GlobalException("更新秒杀数据失败");
        }*/
    }

    public SeckillItem querySeckillItemByBarCodeAndSeckillId(String barCode,String seckillId){
        SeckillItem seckillItem = new SeckillItem();
        seckillItem.setBarcode(barCode);
        seckillItem.setSeckillId(seckillId);
        return selectOne(seckillItem);
    }

    /**
     * 释放商品锁定
     * @return
     */
    public void releaseLockStock(QuerySeckillBo bo){
        SeckillItem seckillItem =  new SeckillItem();
        seckillItem.setIsReleaseStock(bo.getIsReleaseStock());
        seckillItem.setGmtModified(Instant.now());
        Example example = new Example(SeckillItem.class);
        example.and()
                .andEqualTo("seckillId",bo.getSeckillId())
                .andIn("seckillId",bo.getSeckillIds())
                .andEqualTo("id",bo.getId())
                .andIn("id",bo.getIds());
        baseMapper.updateByExampleSelective(seckillItem, example);
    }

    /**
     * 释放还原库存
     * @param seckillId
     * @param skuCode
     * @param qty
     */
    public void releaseLockStock(String seckillId,String skuCode,Integer qty){
        if(baseMapper.releaseSeckillItemQty(seckillId, skuCode,qty)<1){
            throw new SqlDataException(StrUtil.format("释放库存失败 seckillId={},skuCode={},qty={}",seckillId,skuCode,qty));
        }
    }

    /**
     * 扣除购物库存
     * @return
     */
    public void lockSeckillItemQty(String seckillId,String skuCode,Integer qty){
        if(baseMapper.lockSeckillItemQty(seckillId, skuCode,qty)<1){
            throw new SqlDataException(StrUtil.format("锁定库存失败 seckillId={},skuCode={},qty={}",seckillId,skuCode,qty));
        }
    }

    /**
     * 查询秒杀商品
     * @return
     */
    public List<SeckillItemVo> querySeckillItem(QuerySeckillBo bo){
        return baseMapper.querySeckillItem(bo);
    }

    /**
     * 校验商品售卖数量
     * @return
     */
    public List<Map> checkSeckillItemQty(QuerySeckillBo bo){
        return baseMapper.checkSeckillItemQty(bo);
    }

}